package com.xingjibase.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import com.fileutils.SPUtils
import com.google.gson.Gson
import com.tools.R
import com.xingjibase.BaseConstant
import com.xingjibase.api.Http
import com.xingjibase.bean.DeviceBean
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody
import java.io.IOException
import java.util.*


abstract class ActivityXJBase : ActivityInit() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(parView())

        setTranslucent(0)
        onCreate()
    }

    private fun parView(): LinearLayout {
        val params =
            LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        val parentView = LinearLayout(this)
        parentView.orientation = LinearLayout.HORIZONTAL
        parentView.layoutParams = params
        parentView.setBackgroundColor(resources.getColor(setBgColor()))

        val childView = setContentView()
        val chileParams =
            LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        childView.layoutParams = chileParams
        parentView.addView(childView)
        return parentView
    }

    protected open fun setBgColor(): Int {
        return R.color.colorMain
    }

    override fun onResume() {
        super.onResume()
        Http.refreshStatus(this@ActivityXJBase, mRefresh)
    }

    /**
     * 刷新登录状态
     *
     * @param mRefresh
     */
    private val mRefresh: Observer<ResponseBody?> = object : Observer<ResponseBody?> {
        override fun onSubscribe(d: Disposable) {}
        override fun onComplete() {}
        override fun onNext(responseBody: ResponseBody) {
            try {
                val valueStr = responseBody.string()
                val deviceBean: DeviceBean = Gson().fromJson(valueStr, DeviceBean::class.java)
                if (deviceBean.getReterr().equals(Http.SUCCESS)) {
                    if (deviceBean.getUsr() == null) {
                        Toast.makeText(this@ActivityXJBase, "无效的SessionID，请重新登录", Toast.LENGTH_SHORT).show()
                        SPUtils.clear(this@ActivityXJBase)
                        Timer().schedule(object : TimerTask() {
                            override fun run() {
                                try {
                                    startActivity(Intent(baseContext, Class.forName(BaseConstant.LOGINACTIVITY)))
                                    exitAllActivity(Class.forName(BaseConstant.LOGINACTIVITY))
                                } catch (e: ClassNotFoundException) {
                                    e.printStackTrace()
                                }
                            }
                        }, 2000)
                    }
                } else {
                    Toast.makeText(this@ActivityXJBase, "登陆信息获取失败", Toast.LENGTH_SHORT).show()
                }
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }

        override fun onError(e: Throwable) {
            Log.d("DeviceBean", e.message.toString())
            //Toast.makeText(this@PageActivity, "登陆信息获取失败", Toast.LENGTH_SHORT).show()
        }
    }

    protected abstract fun setContentView(): View
    protected abstract fun onCreate()
}